/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.esi.athanfree.modele;

import be.esi.athanfree.bd.Haddith;
import be.esi.athanfree.exception.AthanException;
import com.ahmedsoliman.devel.jislamic.DayPrayers;
import com.ahmedsoliman.devel.jislamic.JIslamic;
import com.ahmedsoliman.devel.jislamic.Method;
import com.ahmedsoliman.devel.jislamic.astro.Location;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Observable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.Timer;

/**
 *
 * @author Yacine
 */
public class AthanModele extends Observable {

    private JIslamic jislamic;
    private Haddith haddith;
    private Timer timer;
    private GregorianCalendar date;
    private DayPrayers prayers;
    private String heure;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("AthanFreePU");
    private EntityManager em = emf.createEntityManager();

    public AthanModele(Location loc) throws AthanException {
        if (loc == null) {
            //TODO Essayer de charger la location depuis un .properties ?
            throw new AthanException("Location is null");
        }
        date = new GregorianCalendar();
        heure = new java.util.Date().toString().substring(11, 19);
        jislamic = new JIslamic(loc, Method.MUSLIM_LEAGUE);
        prayers = jislamic.getPrayerTimes(date);
        // haddith = "Ceci est un test";
        timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        timer.start();
    }

    private void update() {
        //mise à jour de l'heure et recalculer les prières si la date a changer
        java.util.Date uDate = new java.util.Date();
        heure = uDate.toString().substring(11, 19);
        if (heure.equals("00:00:00")) {
            date = new GregorianCalendar();
            prayers = jislamic.getPrayerTimes(date);
        }
        setChanged();
        notifyObservers();
    }

    public GregorianCalendar getDate() {
        return date;
    }

    public void setDate(GregorianCalendar date) {
        this.date = date;
        notifyObservers();
    }

    public Haddith getHaddith() {
        Query qry = em.createNamedQuery("Haddith.findAll");
        List<Haddith> col = qry.getResultList();
        return col.get(0);
    }

    public void setHaddith(Haddith haddith) {
        this.haddith = haddith;
        notifyObservers();
    }

    public DayPrayers getPrayers() {
        return prayers;
    }

    public void setPrayers(DayPrayers prayers) {
        this.prayers = prayers;
        notifyObservers();
    }

    public String getHeure() {
        return heure;
    }

    public void nextHaddith() {
        //TODO Aller cherche un haddith au hasard dans la BD
        notifyObservers();
    }
}
